﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Teamcenter.ClientX;
using Teamcenter.Schemas.Soa._2006_03.Exceptions;
using Teamcenter.Services.Strong.Core._2006_03.DataManagement;
//Include the Saved Query Service Interface
using Teamcenter.Services.Strong.Query;

// Input and output structures for the service operations 
// Note: the different namespace from the service interface
using Teamcenter.Services.Strong.Query._2006_03.SavedQuery;

using Teamcenter.Services.Strong.Core;
using Teamcenter.Soa.Client.Model;

using ImanQuery = Teamcenter.Soa.Client.Model.Strong.ImanQuery;
using SavedQueriesResponse = Teamcenter.Services.Strong.Query._2007_09.SavedQuery.SavedQueriesResponse;
using QueryInput = Teamcenter.Services.Strong.Query._2008_06.SavedQuery.QueryInput;
using QueryResults = Teamcenter.Services.Strong.Query._2007_09.SavedQuery.QueryResults;
namespace TC4T_WinForm
{
    public partial class Form1 : Form
    {
        static Teamcenter.ClientX.Session _LocalSession = null;
        public static RichTextBox RichTextBox=null;
        static log4net.ILog LOGGER = log4net.LogManager.GetLogger(typeof(Form1));
        public Form1()
        {
            InitializeComponent();
            RichTextBox = this.rtb_tcinfo;
        }

        private void bt_login_Click(object sender, EventArgs e)
        {
            if (tb_webserviceuil.Text != string.Empty)
            {
                try
                {
                    if (_LocalSession == null)
                    {
                        MyTCSession.InitSession();
                        _LocalSession = MyTCSession.MySession;
                    }
                }
                catch (Exception ex)
                {
                    this.rtb_tcinfo.Text += ex.Message;
                }
            }
        }

        private void bt_getItemTypes_Click(object sender, EventArgs e)
        {
            //DataManagementService dmService = DataManagementService.getService(MyTCSession.MySessionConnection);
            try
            {
            //    Teamcenter.Services.Strong.Core._2008_06.DataManagement.BOWithExclusionIn[] Input = new Teamcenter.Services.Strong.Core._2008_06.DataManagement.BOWithExclusionIn[1];
            //    Teamcenter.Services.Strong.Core._2008_06.DataManagement.BOWithExclusionIn bow = new Teamcenter.Services.Strong.Core._2008_06.DataManagement.BOWithExclusionIn();
            //    bow.BoTypeName = "Item";
            //    string[] exclusives = { "EngChange", "Part", "Design", "PSSignal", "PSConnection", "Schedule"
            //    ,"ScheduleTask","Architecture","AllocationMap","CAEItem","CAEBCItem","CAEConnItem","CAELoadItem","MEOP",
            //    "MEProcess","MEWorkarea","MEContext","MEGenericBOP","SwDesignComp","PhysicalPart","ChangeItem","ParmDef"
            //                          ,"ParmDefBCD","ParmDefBitDef","ParmDefBool","ParmDefDate","ParmDefDbl","ParmDefHex"
            //                          ,"ParmDefInt","ParmDefSED","ParmDefStr","ParmGrpVal","CalParm","ParmGrpDef","ValidationAgent"
            //                          ,"ValData","CondValAgent","CondValData","Drawing","SSP0Frequency","ValidationReq"
            //                          ,"DesignReq","Mes0MESTXLibrary","Mes0MESTXFolder","Mes0MESTXElement","Mes0MEWITemplate"
            //                          ,"Mes0MEWIObjTempl","TransltnOffice","DC_ExtEntAdmin","DC_Procedure","DCt_Item"
            //                          ,"DC_ComposedDoc","DC_ReviewerDoc","DC_Publication","GraphicOption","DC_Topic"};
            //    bow.ExclusionBOTypeNames = exclusives;
            //    Input[0] = new Teamcenter.Services.Strong.Core._2008_06.DataManagement.BOWithExclusionIn();
            //    Input[0] = bow;

            //    Teamcenter.Services.Strong.Core._2010_04.DataManagement.DisplayableSubBusinessObjectsResponse response = dmService.FindDisplayableSubBusinessObjectsWithDisplayNames(Input);
            //    if (response.Output != null && response.Output.Length > 0)
            //    {
            //        ItemDisplayName.DisplayItems.Clear();
            //        foreach (Teamcenter.Services.Strong.Core._2010_04.DataManagement.BusinessObjectHierarchy h in response.Output[0].DisplayableBOTypeNames)
            //        {
            //            if(LOGGER.IsInfoEnabled)
            //                LOGGER.Info("显示名称:" + h.BoDisplayName + " 对象名称:" + h.BoName);

            //            ItemDisplayName.DisplayItems.Add(h.BoDisplayName);
            //        }
            //    }
                ItemDisplayName itemHyrachy = TCHelper.GetItemHyrachy("item");
            }
            catch (Exception ex)
            {
                LOGGER.Error(ex);
            }
        }

        private void bt_createObject_Click(object sender, EventArgs e)
        {
            if (MyTCSession.MySessionConnection == null)
            {
                MessageBox.Show("先登陆！");
                return;
            }
            CreateItemsResponse outp = TCHelper.CreateItems(1, "My2_TestItem","xiaojin","肖劲的item");
            ModelObject _model = outp.ServiceData.GetCreatedObject(0);
            Teamcenter.Soa.Client.Model.ServiceData data = TCHelper.GetPropertyValues(new ModelObject[] { _model }, new string[] { "item_id" });
            string temp = data.GetPlainObject(0).GetProperty("item_id").StringValue;
            MessageBox.Show(temp);
        }

        private void bt_query_Click(object sender, EventArgs e)
        {
            PromptForm _p = new PromptForm();
            if (_p.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                _p.Close();
        }

        private void bt_setProperties_Click(object sender, EventArgs e)
        {

            Dictionary<string, string[]> attname = new Dictionary<string, string[]>();
            ModelObject mo1 = TCHelper.QueryItemByItemID("000095");
            ModelObject mo2 = TCHelper.QueryItemByItemID("000096");
            string[] temp = new string[] { mo1.Uid, mo2.Uid };
            attname.Add("my2_typearray", temp);
            TCHelper.SetPropertyValue("000091", attname);
        }
    }
}
